package com.codermn.demo7;

/**
 * @author jxau_scf
 * @date 2021/6/20 - 17:08
 */
public class BaoZiPu extends Thread {
    private BaoZi baoZi;

    public BaoZiPu(BaoZi baoZi){
        this.baoZi = baoZi;
    }

    @Override
    public void run() {
        int count = 0;
        while (true){
            synchronized (baoZi){
                if (baoZi.isFlag() == true){
                    try {
                        baoZi.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    if (count % 2 == 0){
                        baoZi.setPi("薄皮");
                        baoZi.setXian("三鲜");
                    }else {
                        baoZi.setPi("冰皮");
                        baoZi.setXian("牛肉");
                    }
                    count++;
                    System.out.println("包子铺正在生产"+baoZi.getPi()+baoZi.getXian()+"的包子");
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    baoZi.setFlag(true);
                    baoZi.notify();
                    System.out.println("包子铺已经生产好了"+baoZi.getPi()+baoZi.getXian()+"的包子,吃货可以开吃了");
                }
            }
        }
    }
}
